"""
Case Type   : cm_ctl
Case Name   : 先执行升级命令,再执行暂停命令
Create At   : 2024/04/10
Owner       : @kyezele
Description :
    1.取最新包在主节点升级预安装;
    2.在主节点执行升级;
    3.执行暂停
Expect      :
    1.预安装成功
    2.成功
    3.暂停成功
History     :
"""
import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2环境不执行')
class DefinedResource(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.com = Common()
        self.constant = Constant()
        self.pri_root = Node('PrimaryRoot')
        self.pri_user = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.upgrade_pkg = os.path.join(macro.DB_SCRIPT_PATH, '..', '..',
                                        'upgrade', 'pkg')

        text = '----查看用户属组----'
        self.log.info(text)
        user_groups = f'groups {self.pri_user.ssh_user}'
        self.log.info(user_groups)
        groups_msg = self.pri_root.sh(user_groups).result()
        self.log.info(groups_msg)
        self.assertIn(f'{self.pri_user.ssh_user}', groups_msg,
                      '执行失败:' + text)
        self.group = groups_msg.split()[2]

    def test_cm(self):
        text = '----step1.1:获取最新包 expect:成功----'
        self.log.info(text)
        self.com.check_load_msg(self.pri_root, self.pri_user, self.upgrade_pkg,
                                '')

        text = '----step1.2:修改文件属性 expect:修改成功----'
        self.log.info(text)
        chown = f'chown -R {self.pri_user.ssh_user}:{self.group} ' \
            f'{self.upgrade_pkg}'
        self.log.info(chown)
        status_msg = self.com.get_sh_result(self.pri_root, chown)
        self.assertEqual('', status_msg, '执行失败:' + text)

        text = '----step1.3:在主节点升级预安装 expect:预安装成功----'
        self.log.info(text)
        upgrade_script = os.path.join(self.upgrade_pkg, 'script')
        preinstall_cmd = f'./gs_preinstall -U {self.pri_user.ssh_user} ' \
            f'-G {self.group} -X {macro.DB_XML_PATH}' \
            f' --sep-env-file={macro.DB_ENV_PATH} --skip-hostname-set '
        execute_cmd = self.com.do_preinstall(self.pri_root, upgrade_script,
                                             preinstall_cmd)
        self.assertIn(self.constant.preinstall_success_msg, execute_cmd,
                      '执行失败:' + text)

        text = '----step2:执行gs_upgradectl升级 expect:升级失败----'
        self.log.info(text)
        param = f"-t auto-upgrade -X {macro.DB_XML_PATH}"
        exec_msg = self.com.do_upgrade(self.pri_root, self.pri_user.ssh_user,
                                       macro.DB_ENV_PATH, param)
        self.assertIn('Failed to start cluster', exec_msg, text + '执行失败')

        text = '----step3:执行cm_ctl pause; expect:暂停成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('pause')
        self.assertIn(self.constant.cm_pause_success_msg, cm_ctl_res,
                      '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text1 = '----恢复集群状态; expect:执行成功----'
        self.log.info(text1)
        cm_ctl_res1 = self.primary_sh.exec_cm_ctl('resume')
        cm_ctl_res2 = self.primary_sh.exec_cm_ctl('start')
        time.sleep(10)
        cm_ctl_res3 = self.primary_sh.exec_cm_ctl('query', '-Civ')

        text2 = '----升级回退 expect:成功----'
        self.log.info(text2)
        param = f"-t auto-rollback -X {macro.DB_XML_PATH}"
        rollback_res = self.com.do_upgrade(
            self.pri_root, self.pri_user.ssh_user, macro.DB_ENV_PATH, param)

        text3 = '----恢复预安装 expect:预安装成功----'
        self.log.info(text3)
        preinstall_cmd = f'./gs_preinstall -U {self.pri_user.ssh_user} ' \
            f'-G {self.group} -X {macro.DB_XML_PATH}' \
            f' --sep-env-file={macro.DB_ENV_PATH} --skip-hostname-set '
        preinstall_res = self.com.do_preinstall(
            self.pri_root, macro.DB_SCRIPT_PATH, preinstall_cmd)
        self.assertIn(self.constant.upgrade_rollback_success_msg, rollback_res,
                      text1 + '执行失败')
        self.assertIn(self.constant.preinstall_success_msg, preinstall_res,
                      '执行失败:' + text2)
        self.assertIn(self.constant.cm_resume_success_msg, cm_ctl_res1,
                      '执行失败:' + text3)
        self.assertIn(self.constant.cm_start_success_msg, cm_ctl_res2,
                      '执行失败:' + text3)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, cm_ctl_res3,
                      "执行失败:" + text3)
        self.log.info(f'----{os.path.basename(__file__)} finish----')